'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 公会列表
 * @author sundbcn@gmail.com (sun debing)
 */

var async = require('async');
var _ = require('lodash');
var Code = require('../../../../consts/code');
var unionGroupService = require('../../../../services/unionGroupService');
var unionGroupModel = require('../../../../models/unionGroupModel.js');
var unionGroupConf = require('../../../../config/unionGroupConf.js');
var PlayerManager = require('../../../../libs/playerManager.js');

/**
 * 公会列表
 */
module.exports = function (msg, session, next) {
    var role_id = session.uid;
    var union_list = [];

    var player_manager = new PlayerManager(role_id);
    var curr_union_info;
    async.waterfall(
        [
            // function (cb) {
            //     player_manager.refreshData(cb);
            // },
            function (cb) {
                player_manager.getModel(['role'], cb);
            },
            function (cb) {
                var role_model = player_manager.role;
                var union_id = role_model.union_id;
                _getUnionInfo(union_id, cb);
            },
            function (union_info, cb) {
                curr_union_info = union_info;
                unionGroupModel.getUnionList(cb);
            },
            function (union_list_all, cb) {
                union_list_all = _.slice(union_list_all, 0, 20);
                var rank_num = 0;
                _.forEach(union_list_all, function (item) {
                    rank_num++;
                    union_list.push({
                        union_id: item.union_id,
                        name: item.name,
                        lv: item.lv,
                        union_flag: item.union_flag,
                        member_count: item.union_user_list.length,
                        union_exp: item.union_exp,
                        union_announce: item.union_announce,
                        rank: rank_num
                    });
                });
                cb();
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK,
                result: {
                    curr_union_info: curr_union_info,
                    union_list: union_list
                }
            });
        }
    );
};

/**
 * 查询当前玩家联盟信息
 */
var _getUnionInfo = function (union_id, cb) {
    var no_union = {
        union_id: 0,
        name: '',
        lv: 0,
        union_flag: 0,
        member_count: 0,
        union_exp: 0,
        union_announce: '',
        rank: 0
    };
    if (union_id === 0) {
        return cb(null, no_union);
    }
    unionGroupModel.getById(union_id, function (err, model) {
        if (!!err) {
            console.error(err);
            return cb(err, no_union);
        }
        var info = {
            union_id: model.union_id,
            name: model.name,
            lv: model.lv,
            union_flag: model.union_flag,
            member_count: model.union_user_list.length,
            union_exp: model.union_exp,
            union_announce: model.union_announce
        };

        unionGroupModel.getRankByUnionId(union_id, info.union_exp, function (err, rank_num) {
            if (!!err) {
                console.error(err);
                return cb(err, no_union);
            }
            info.rank = rank_num;
            cb(err, info);
        });
    });
};
